import java.net.*;
import java.io.*;

import java.net.*;
import java.io.*;

/**

**/
class SocketMulti extends Thread {
	Deck deck;
	Auction auction;
	Table table;

    PrintWriter out = null;
    BufferedReader in = null;
    private Socket socket = null;
    int pn;
    String small;
    String big;
    String cash;
    
    


    public void player(int pn,String small, String big,String cash, Deck deck, Auction auction, Table table){
                String[] playerhand=null;
                String winnings;
                int k=0;
                String crd="";
        String ncrd="";
        int a;
                int playercash;
                String [] help=null;
                String[] newcards=null;
                String Allin="0";
                int Allinvalue=0;
                String ID="0";
                String line;
                String wait="2";
                String id;
                int game=0;

                try {
                        out.println("You have joined this table");
                        ID =table.getID();
                        out.println(ID);
                        playercash = Integer.parseInt(table.startingcash);
                        out.println(table.startingcash);
                        if(ID.equals("0")){
                        out.println("To many players try again");
                        this.socket.close();
                        this.in.close();
                        this.out.close();
                        
                        }
                        out.println("Waiting for other players,press ready");
                        id=in.readLine();
                        while(!wait.equals("1"))
                       wait=table.wait(id);
                        wait="2";
                        System.out.println(ID);
                        while(table.number>1 && game<3){
                        	out.println( "All players ready press start");

                        	help=table.round0(in.readLine()).split(" ");
                        	System.out.println(help[0]);
                        	if((help[0].equals("B"))||(help[0].equals("S"))){
                        		playercash=playercash-Integer.parseInt(help[1]);

                        		auction.pot=auction.pot+Integer.parseInt(help[1]);
            }
            out.println(help[0]+" "+help[1]+" "+help[2]+" press_ready");
            help=null;
            out.println(auction.pot);

            id=in.readLine();
            while(!wait.equals("1"))
                wait=table.wait(id);
                 wait="2";
            System.out.println(ID);
        	out.println("All players ready press Get cards");
            playerhand=deck.dealing(4).split(" ");
            for(int i=0; i<4; i++)
            System.out.println(playerhand[i]);
            out.println(playerhand[0]+" "+playerhand[1]+" "+playerhand[2]+" "+playerhand[3]+" ");
            out.println(""+auction.pot);
            out.println(""+auction.maxbet);
            out.println("Waiting for other players, press ready");
            id=in.readLine();
            while(!wait.equals("1"))
                wait=table.wait(id);
                 wait="2";

            for(int js=0 ; js<3; js++){
            	if(playercash==0 && Allin.equals("0")){
            		 table.deleteplayer(ID);
                     out.println("You lose");
                     this.socket.close();
                     this.in.close();
                     this.out.close();
            		
            	}
             out.println("All players ready, it's time to bet");
            line = in.readLine();
            System.out.println(line);
            help=line.split(" ");
            if(help[0].equals("Allin")){
                Allin="1";
                Allinvalue=Integer.parseInt(help[1]);
                playercash=0;
                auction.Round(help[1]);
                help=null;
            }
            else if(help[0].equals("Fold")){
                table.deleteplayer(ID);
                out.println("You lose");
                this.socket.close();
                this.in.close();
                this.out.close();
                
            }
            else{
                auction.Round(help[1]);
                playercash=playercash-Integer.parseInt(help[1]);
                help=null;
            }
            out.println("Waiting for other players, press ready");
            System.out.println(table.number);
            id=in.readLine();
            if(table.number>1){
            while(!wait.equals("1"))
                wait=table.wait(id);
                 wait="2";
                 System.out.println(table.number);
            out.println("All players ready, Choose cards to exchange" );

            line=in.readLine();
            if(!line.equals("")){
            help=line.split(" ");
            a=help.length;
           
            for(int i=0; i<a; i++){
                crd=crd+help[i]+" ";
            }
            newcards=deck.change(crd).split(" ");
            for(int i=0; i<4; i++){
                for(int j=0; j<a; j++){
                        if(playerhand[i].equals(help[j])){
                                playerhand[i]=newcards[k];
                                k++;
                        }
                }
            }
            for(int i=0; i<a; i++){
                ncrd=ncrd+newcards[i]+" ";
            }

            System.out.println(ncrd+"a");
            out.println(ncrd);
            }
            out.println(""+auction.pot);
            out.println(""+auction.maxbet);
            out.println("Waiting for other players, press ready");
            id=in.readLine();
            while(!wait.equals("1"))
                wait=table.wait(id);
                 wait="2";

            k=0;
            ncrd="";
            crd="";
            newcards=null;
                 }
                 else
                	 break;
            }
            
            out.println("Send your cards");
            line=in.readLine();
            System.out.println(line);
            table.hierarchy(line);
            out.println("Waiting for other players, press ready");
            id=in.readLine();
            if(table.number>1){
            while(!wait.equals("1"))
                wait=table.wait(id);
                 wait="2";
            }
                 out.println("All players ready, press result");
            help=table.payment(ID+" "+ in.readLine()+" "+Allin+" "+"Allinvalue").split(" ");
            System.out.println(help[0]);
            System.out.println(table.sorted[0][0]);
            if(help[0].equals("N")){
                if(help[1].equals("1")){
                        out.println("It's_a_draw 0"+ " press_ready");

                        
                }
                else{
                        out.println("You_lost 0"+" press_ready");

                }
            }
            else{
                winnings=auction.givecash(help[1]+" "+help[2]+" "+help[3]);
                playercash=playercash+Integer.parseInt(winnings);
                out.println("You_won "+winnings+" press_ready");

                
            }
            out.println(auction.pot);
            id=in.readLine();
            if(table.number>1){
            while(!wait.equals("1"))
                wait=table.wait(id);
                 wait="2";
            }
            game++;
                }
                        if(table.number>1){
                            while(!wait.equals("1"))
                                wait=table.wait(id);
                                 wait="2";
                            }
                   out.println("Game over");     
                      
                }
        catch (IOException e) {
           System.out.println(ID+" zrezygnowal"); 
        }

                
        }

    
 
    public SocketMulti(Socket socket,int n,String s, String b, String c,Deck deck, Auction auction, Table table){
    	this.deck=deck;
    	this.auction=auction;
    	this.table=table;
        pn =n;
        small=s;
        big=b;
        cash=c;
        this.socket = socket;
        start();
    }
 
    public void run() {
        try {
            out = new PrintWriter(socket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            player(pn,small,big,cash,deck,auction,table);
        }
        catch (IOException e) {
            System.out.println("Blad IO danych!");
        }
        finally {
            try {
                if (socket != null) socket.close();
            }
            catch (IOException e) {
                System.out.println("Blad zamkniecia gniazda!");
            }
        }
    }
}

public class Server {

    public static void main(String[] args) throws IOException {

    	Deck deck = new Deck();
    	Auction auction = new Auction();
    	Table table = new Table();
        int tab=0;
            try {
                    tab=Integer.parseInt(args[0]);
                }
            catch (NumberFormatException ex) {
                    System.out.println(args[0]+"-Niepoprawna liczba");
                }
        

        System.out.println("Serwer Aktywny");
        ServerSocket server = new ServerSocket(4444);
        table.createID(tab);
        table.paramsetup(args[1], args[2], args[3], tab);
        deck.shuffle(52);
        try {
            while (true) {
                Socket socket = server.accept();
                new SocketMulti(socket,tab,args[1],args[2],args[3],deck,auction,table);
            }
        }
        finally { server.close();}
    }
}